<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .outer_container{
            width: 400px;
            height: 180px;
            border: 1px solid black;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            margin-top: 30px;
        }
        table{
            width: 400px;
            height: 180px;
            border: 1px solid black;
            border-collapse:collapse
        }
        tr ,td{
            border: 1px solid black;
            text-align: center;
        }
        button{
            width: 20px;
            height: 20px;
            border-radius: 10px;
            text-align: center;
            line-height: 6px;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="outer_container">
        <div class="main_content">
            <table>
                <tr>
                    <td colspan="4">购物车</td>
                </tr>
                <tr>
                    <td>商品名称</td>
                    <td>单价</td>
                    <td>数量</td>
                    <td>小计</td>
                </tr>
                <tr>
                    <td>iphone13</td>
                    <td>4999</td>
                    <td>
                        <button>-</button>
                        <input type="text" value = "0" style="width: 20px;text-align: center;">
                        <button>+</button>
                    </td>
                    <td class="td1 tdd"></td>
                </tr>
                <tr>
                    <td>HuaWei P50</td>
                    <td>5999</td>
                    <td>
                        <button>-</button>
                        <input type="text" value = "0" style="width: 20px;text-align: center;">
                        <button>+</button>
                    </td>
                    <td class="td2 tdd"></td>
                </tr>
                <tr>
                    <td>XiaoMi 13</td>
                    <td>3999</td>
                    <td>
                        <button>-</button>
                        <input type="text"  value = "0" style="width: 20px;text-align: center;">
                        <button>+</button>
                    </td>
                    <td class="td3 tdd"></td>
                </tr>
                <tr>
                    <td>三只松鼠大礼包</td>
                    <td>99</td>
                    <td>
                        <button>-</button>
                        <input type="text" value = "0" style="width: 20px;text-align: center;">
                        <button>+</button>
                    </td>
                    <td class="td4 tdd"></td>
                </tr>
                <tr>
                    <td colspan="3">总计</td>
                    <td class="td5 tdd"></td>
                    
                </tr>
            </table>
        </div>
    </div>
    <script>
        let btns = document.getElementsByTagName("button")
        let inp = document.getElementsByTagName("input")
        let tds = document.getElementsByClassName("tdd")
        let tables = document.getElementsByTagName("table")
        console.log(tds);
        console.log(tables)
        console.log(tables[0].tBodies[0].rows[2].cells[3]);
        // 第一行商品
        btns[0].onclick = function(){
            if(inp[0].value <= 0){
                inp[0].value = 0
            }else{
                inp[0].value = inp[0].value - 1
            }
            tds[0].innerHTML = `${inp[0].value * 4999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        btns[1].onclick = function(){
            inp[0].value = +(inp[0].value) + 1
            tds[0].innerHTML = `${inp[0].value * 4999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }


        // 第二行商品
        btns[2].onclick = function(){
            if(inp[1].value <= 0){
                inp[1].value = 0
            }else{
                inp[1].value = inp[1].value - 1
            }
            tds[1].innerHTML = `${inp[1].value * 5999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        btns[3].onclick = function(){
            inp[1].value = +(inp[1].value) + 1
            tds[1].innerHTML = `${inp[1].value * 5999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        // 第三行商品
        btns[4].onclick = function(){
            if(inp[2].value <= 0){
                inp[2].value = 0
            }else{
                inp[2].value = inp[2].value - 1
            }
            tds[2].innerHTML = `${inp[2].value * 3999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        btns[5].onclick = function(){
            inp[2].value = +(inp[2].value) + 1
            tds[2].innerHTML = `${inp[2].value * 3999}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        // 第四行商品
        btns[6].onclick = function(){
            if(inp[3].value <= 0){
                inp[3].value = 0
            }else{
                inp[3].value = inp[3].value - 1
            }
            tds[3].innerHTML = `${inp[3].value * 99}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        btns[7].onclick = function(){
            inp[3].value = +(inp[3].value) + 1
            tds[3].innerHTML = `${inp[3].value * 99}`
            tds[4].innerHTML = `${
                                    inp[0].value * 4999 + inp[1].value * 5999 + inp[2].value * 3999 + inp[3].value * 99
                                }`
        }

        
    </script>
</body>
</html>